package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.OperSerialQueryResponse;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @PROJECT_NAME: SGW_2.1
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/19 14:45
 */
public class OperSerialQueryBizSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory.getLogger(OperSerialQueryBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();

        String startMonth = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Start_Month");
        msgBody.put("startMonth", startMonth);
        String endMonth = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/End_Month");
        msgBody.put("endMonth", endMonth);
        String objValue = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Destination_Id");
        String objAttr = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/DestinationAttr");

        Map<String, String> operAttrStruct = new HashMap<>();
        operAttrStruct.put("operServiceId", "1");
        operAttrStruct.put("lanId", "8420100");
        operAttrStruct.put("operPost", "0");
        operAttrStruct.put("operOrgId", "0");
        operAttrStruct.put("staffId", "0");
        operAttrStruct.put("operTime", "0");
        msgBody.put("operAttrStruct", operAttrStruct);

        Map<String, String> svcObjectStruct = new HashMap<>();
        svcObjectStruct.put("objValue", objValue);
        svcObjectStruct.put("objAttr", objAttr);
        svcObjectStruct.put("objType", "3");
        svcObjectStruct.put("dataArea", "0");
        msgBody.put("svcObjectStruct", svcObjectStruct);

        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

        LOG.info("request body is {}", msgBodyStr.toString());
        LOG.info("webserviceUrl is {}", url);
        String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), url);
        LOG.info("response is {}", respStr);

        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        OperSerialQueryResponse operSerialQueryResponse = new Gson().fromJson(respStr, OperSerialQueryResponse.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        respDataXml.put("Result", operSerialQueryResponse.getResultCode());
        respDataXml.put("Para_Field_Result", operSerialQueryResponse.getResultMsg());
        respDataXml.put("Sum_Cnt", operSerialQueryResponse.getSumCnt());

        List<OperSerialQueryResponse.BillInformation> billInformationList = operSerialQueryResponse.getBillInformationList();
        String bodyItem = "";
        if (billInformationList != null) {
            for (OperSerialQueryResponse.BillInformation query : billInformationList) {
                SortedMap<Object, Object> billQueryInfoXml = new TreeMap<>();
                billQueryInfoXml.put("Acct_ID", query.getAcctId());
                billQueryInfoXml.put("Payment_Serial_Nbr", query.getPaymentSerialNbr());
                billQueryInfoXml.put("Oper_Type", query.getOperType());
                billQueryInfoXml.put("Oper_Des", query.getOperDes());
                billQueryInfoXml.put("Payment_state", query.getPaymentState());
                billQueryInfoXml.put("Pay_Type_Name", query.getPayTypeName());
                billQueryInfoXml.put("StartTime", query.getStartTime());
                billQueryInfoXml.put("AMOUNT", query.getAmount());
                billQueryInfoXml.put("Staff_Id", query.getStaffId());
                billQueryInfoXml.put("Site_Id", query.getSiteId());
                billQueryInfoXml.put("Latn_Id", query.getLanId());
                billQueryInfoXml.put("Billing_Cycle", query.getBillingCycleId());
                try {
                    bodyItem+="<Bill_Information>"+HttpClientUtils.mapToSignXml(billQueryInfoXml)+"</Bill_Information>";
                } catch (Exception e) {
                    bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                    LOG.error("Stitching XML error", e);
                    return bizResult;
                }
            }
        }
        String respXmlStr = "";
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respDataXmlStr += bodyItem;
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            LOG.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }
}
